package ai.people.platform.archives.tree;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhenglin
 * @date 2022/7/27
 */
public class TreeNodeUtil {


    public static boolean contains(TreeNode root, String name) {
        if (root == null) {
            return false;
        }
        if (root.getName().equals(name)) {
            return true;
        }
        for (TreeNode child : root.getChildren()) {
            if (contains(child, name)) {
                return true;
            }
        }
        return false;
    }


    public static TreeNode getByName(TreeNode root, String name) {
        if (root == null) {
            return null;
        }
        if (root.getName().equals(name)) {
            return root;
        }
        for (TreeNode child : root.getChildren()) {
            TreeNode node = getByName(child, name);
            if (node != null) {
                return node;
            }
        }
        return null;
    }

    public static void recursion(TreeNode root) {
        System.out.println(root.getName());
        for (TreeNode child : root.getChildren()) {
            recursion(child);
        }
    }

    public static void bfs(TreeNode root) {
        Deque<TreeNode> nodeDeque = new LinkedList<>();
        TreeNode node = root;
        nodeDeque.add(node);
        while (!nodeDeque.isEmpty()) {
            node = nodeDeque.pop();
            System.out.println(node.getName());
            nodeDeque.addAll(node.getChildren());
        }
    }

    public static void dfs(TreeNode root) {
        Deque<TreeNode> nodeDeque = new LinkedList<>();
        TreeNode node = root;
        nodeDeque.push(node);
        while (!nodeDeque.isEmpty()) {
            node = nodeDeque.pop();
            System.out.println(node.getName());
            List<TreeNode> children = node.getChildren();
            //注意这里要从后向前遍历
            for (int i = children.size() - 1; i >= 0; i--) {
                nodeDeque.push(children.get(i));
            }
        }
    }

    public static TreeNode makeTree() {
        TreeNode root = new TreeNode("A", null);

        TreeNode B = new TreeNode("B", root);
        TreeNode C = new TreeNode("C", root);
        TreeNode D = new TreeNode("D", root);

        root.addChild(B);
        root.addChild(C);
        root.addChild(D);


        TreeNode E = new TreeNode("E", B);
        TreeNode F = new TreeNode("F", B);

        B.addChild(E);
        B.addChild(F);

        TreeNode G = new TreeNode("G", D);

        D.addChild(G);


        TreeNode H = new TreeNode("H", E);
        TreeNode I = new TreeNode("I", E);
        TreeNode J = new TreeNode("J", E);

        E.addChild(H);
        E.addChild(I);
        E.addChild(J);

        return root;
    }

    public static void main(String[] args) {
        TreeNode root = makeTree();
        //recursion(root);
        //bfs(root);
        dfs(root);

        boolean i = contains(root, "I");
        System.out.println(i);


        TreeNode i1 = getByName(root, "I");
        System.out.println(i1.getParent().getName());
    }
}
